package pers.corvey.crawler.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import pers.corvey.crawler.model.Movie;
import pers.corvey.crawler.model.json.TaskResultJson;
import pers.corvey.crawler.service.MovieService;

@Controller
public class MovieController {

	@Autowired
	private MovieService movieService;
	
	@RequestMapping("/")
	public String index() {
		return "/index.html";
	}
	
	@PostMapping("/search")
	@ResponseBody
	public TaskResultJson search(String keyword, String tag) {
		long begin = System.currentTimeMillis();
		List<Movie> movies;
		try {
			movies = movieService.crawlBySearch(keyword, tag);
		} catch (Exception e) {
			e.printStackTrace();
			return throwException(begin);
		}
		TaskResultJson ret = new TaskResultJson();
		ret.setStatus(true);
		long end = System.currentTimeMillis();
		ret.setTime(end - begin);
		ret.setMsg("成功抓取" + movies.size() + "条电影记录");
		return ret;
	}
	
	@PostMapping("/intheater")
	@ResponseBody
	public TaskResultJson inTheater(String city) {
		long begin = System.currentTimeMillis();
		List<Movie> movies;
		try {
			movies = movieService.crawlInTheaters(city);
		} catch (Exception e) {
			e.printStackTrace();
			return throwException(begin);
		}
		long end = System.currentTimeMillis();
		TaskResultJson ret = new TaskResultJson();
		ret.setStatus(true);
		ret.setTime(end - begin);
		ret.setMsg("成功抓取" + movies.size() + "条电影记录");
		return ret;
	}
	
	@PostMapping("/comingsoon")
	@ResponseBody
	public TaskResultJson comingSoon() {
		long begin = System.currentTimeMillis();
		List<Movie> movies;
		try {
			movies = movieService.crawlComingSoon();
		} catch (Exception e) {
			e.printStackTrace();
			return throwException(begin);
		}
		long end = System.currentTimeMillis();
		TaskResultJson ret = new TaskResultJson();
		ret.setStatus(true);
		ret.setTime(end - begin);
		ret.setMsg("成功抓取" + movies.size() + "条电影记录");
		return ret;
	}
	
	@PostMapping("/top250")
	@ResponseBody
	public TaskResultJson top250() {
		long begin = System.currentTimeMillis();
		List<Movie> movies;
		try {
			movies = movieService.crawlTop250();
		} catch (Exception e) {
			e.printStackTrace();
			return throwException(begin);
		}
		long end = System.currentTimeMillis();
		TaskResultJson ret = new TaskResultJson();
		ret.setStatus(true);
		ret.setTime(end - begin);
		ret.setMsg("成功抓取" + movies.size() + "条电影记录");
		return ret;
	}
	
	private TaskResultJson throwException(long begin) {
		TaskResultJson ret = new TaskResultJson();
		ret.setStatus(false);
		long end = System.currentTimeMillis();
		ret.setTime(end - begin);
		ret.setMsg("服务器发生错误！请重试！");
		return ret;
	}
}
